import axios from "axios";

//
// 163 ->
// 'https://music.163.com/'
async function get163HotPlayList(f){
    await axios.get('/163api/top/playlist?limit=30').then((r) => {
        //console.log(r)
        let result = []
        for (let playlist of r.data.playlists) {
            result.push(deal163Result(playlist))
        }
        //console.log("@@@163",result.length)

        f(result)
    })
}
function deal163Result(data){
    if(data.creator === undefined){
        //console.log(data)
    }
    let {name,id,coverImgUrl:imgUrl,creator:{nickname : auth = ""} = ""} = data;



    return {
        "name":name,
        "id":id,
        "imgUrl":imgUrl,
        "auth":auth,
        "from":'163'
    }
}



async function get163PlayListDetail(id,f){
    await axios.get('/163api/playlist/detail?id='+id).then((r) => {

        //console.log(r.data.playlist)
        let result = deal163DetailResult(r.data.playlist)
        //console.log(result)
        f(result)
    })
}
function deal163DetailResult(data){

    let {creator:{nickname:user},name, id, description,tags, coverImgUrl:img,playCount,subscribedCount} = data;
    let {tracks} = data;
    let r = tracks.map((i) => {
        let ts = i.dt;
        ts = ts / 1000;
        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }

        return{
            'id':i.id,
            'times' : `${min} : ${sec}`,
            'name' : i.name,
            'singer' : i.ar[0].name,
            'album' : i.al.name
        }
    })


    return {
        user:user,
        tags:tags,
        name:name,
        id:id,
        description:description,
        img:img,
        playCount:playCount,
        subscribedCount:subscribedCount,
        list:r
    }
}

/**
 * {
 *                     id: "1",
 *                     title: "打上花火",
 *                     info: "DAOKO,米津玄師",
 *                     // resource: require("./resource/DAOKO,米津玄師 - 打上花火.mp3"),
 *                     resource: "",
 *                     time: "04:50",
 *                     img:
 *                         "http://singerimg.kugou.com/uploadpic/softhead/400/20160913/20160913140233132.jpg"
 *                 },
 */

async function get163PlayMusic(id,f){

    await axios.get('/163api/song/detail?ids='+id).then((r) => {
        if(!r.data.songs[0]){
            f(-1)
        }else {
            let result = deal163PlayMusicResult(r.data.songs[0])
            f(result)
        }
    })
}
function deal163PlayMusicResult(data){

   let {id, name : title} = data
    let info = data.ar[0].name
    let img = data.al.picUrl
    let resource = 'https://music.163.com/song/media/outer/url?id='+id+'.mp3';
    let ts = data.dt;
    ts = ts / 1000;
    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
        min = '0' + min;
    }
    if(sec < 10){
        sec = '0' + sec;
    }
    let time = `${min} : ${sec}`;

    return {
        id:id,
        title:title,
        info:info,
        resource:resource,
        from:'163',
        time:time,
        img:img
    }
}


async function login163Music(phone,pwd){

   return await axios.get(`/163api/login/cellphone?phone=${phone}&password=${pwd}`)
}
async function login163Status(){

    return await axios.get(`/163api/login/status`)
}

async function get163Search(keywords,offset,limit,type, f){

    return await axios.get(`/163api/cloudsearch?keywords=${keywords}&type=${type}&offset=${offset}&limit=${limit}`).then((r) => {
        if(type === 1 || type === '1'){
            //console.log(r)
            let result = deal163SearchSongsResult(r.data.result)
            let count = r.data.result?.songCount || 0
            f({v:result,count:count})
        }else if(type === 100 || type === '100'){
            //console.log(r)
            let result = deal163SearchSingersResult(r.data)
            f(result)
        }else if(type === 1000 || type === '1000'){
            //console.log(r)
            let result = deal163SearchPlayListResult(r.data)
            f(result)
        }else if(type === 10 || type === '10'){
            //console.log(r)
            let result = deal163SearchAlbumResult(r.data)
            f(result)
        }
    })
}
function deal163SearchSongsResult(data){

    let {songs} = data;
    let r = songs.map((i) => {
        let ts = i.dt;
        ts = ts / 1000;
        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }

        return{
            'id':i.id,
            'times' : `${min} : ${sec}`,
            'name' : i.name,
            'singer' : i.ar[0].name,
            'album' : i.al.name,
            'from' : '163'
        }
    })
    return r

}
function deal163SearchSingersResult(data){

    //console.log(data)
    let result = data.result || {artistCount:0,artists:[{}]}
    let map = result.artists.map((r) => {

        return {
            id:r.id,
            name: r.name,
            img: r.picUrl,
            from:'163'
        }
    });
    return map
}

function deal163SearchPlayListResult(data){
    //console.log(data)
    let als = data.result.playlists || []
    return als.map((r) => {

        return {
            "name":r.name,
            "id":r.id,
            "imgUrl":r.coverImgUrl,
            "auth":r.creator.nickname,
            "from":'163'
        }

    })



}

function deal163SearchAlbumResult(data){
    //console.log(data)
    let als = data.result.albums || []
    return als.map((r) => {

        return {
            "name":r.name,
            "id":r.id,
            "img":r.picUrl,
            "auth":r.artist.name,
            "from":'163'
        }

    })



}
async function get163SingerDetail(id,f){
    await axios.get(`/163api/artist/album?id=${id}&limit=100`).then((r) => {

        //console.log(r.data)
        let result = deal163SingerDetailResult(r.data)

        f(result)
    })
}
function deal163SingerDetailResult(data) {
    //console.log(data)
    let als = data.hotAlbums || []

    let result = als.map((r) => {
        return{
            img:r.blurPicUrl,
            id:r.id,
            name:r.name,
            auth:r.artist.name,
            from:'163'
        }
    });

    return result;
}


async function get163AlbumDetail(id,f){
    await axios.get('/163api/album?id='+id).then((r) => {

        //console.log(r.data)
        let result = deal163AlbumDetailResult(r.data)

        f(result)
    })
}
function deal163AlbumDetailResult(data){
    let tracks = data.songs || [];
    let r = tracks.map((i) => {
        let ts = i.dt;
        ts = ts / 1000;
        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }

        return{
            'id':i.id,
            'times' : `${min} : ${sec}`,
            'name' : i.name,
            'singer' : i.ar[0].name,
            'album' : i.al.name
        }
    })


    return r
}
async function get163Catlist(f){

    await axios.get('/163api/playlist/catlist').then((r) => {

        //console.log(r.data)
        let result = deal163Catlist(r.data)

        f(result)
    })
}
function deal163Catlist(data){
    let cat = [];
    let i = 0;
    for (const e of Object.values(data.categories)) {

        cat[i++] = {'name':e,
                    'v':[]
        };
    }

    let subs = data.sub || []
    for (const s of subs) {
        cat[s.category].v.push(s.name);
    }
    //console.log(cat)
    return cat;
}
async function get163CatPlayList(cat,offset,limit,f){
    await axios.get(`/163api/top/playlist?limit=${limit}&offset=${offset}&cat=${cat}`).then((r) => {
        //console.log(r.data)
        let result = deal163CatResult(r.data)
        let count = r.data.total
        f({v:result,count:count})
    })
}
function deal163CatResult(data){
    let playlists = data.playlists || []
    return playlists.map((r) => {

        return {
            "name":r.name,
            "id":r.id,
            "imgUrl":r.coverImgUrl,
            "auth":r.creator.nickname,
            "from":'163'
        }

    })
}

async function get163TopList(f){
    await axios.get(`/163api/toplist`).then((r) => {
        //console.log(r.data)
        let result = deal163TopResult(r.data)

        f(result)
    })
}
function deal163TopResult(data){
    let playlists = data.list || []
    return playlists.map((r) => {

        return {
            "name":r.name,
            "id":r.id,
            "imgUrl":r.coverImgUrl,
            "auth":r.description,
            "from":'163'
        }

    })
}
async function get163SingerByCat(data,page,perPage,f){
    page = (page-1) * perPage
    await axios.get(`/163api/artist/list?type=${data.type}&area=${data.area}&initial=${data.initial}&limit=${perPage}&offset=${page}`).then((r) => {
        //console.log(r.data)
        let result = deal163SingerByCatResult(r.data)
        let more = r.data.more
        f({v:result,more:more})
    })
}
function deal163SingerByCatResult(data){
    let playlists = data.artists || []
    return playlists.map((r) => {

        return {
            id:r.id,
            name: r.name,
            img: r.picUrl,
            from:'163'
        }

    })
}
async function get163lyric(id,f){
    await axios.get(`/163api/lyric?id=${id}`).then((r) => {
        //console.log(r.data)
        let result = deal163lyricResult(r.data)

        f(result)
    })
}
function deal163lyricResult(data){

    return data.lrc.lyric || ''

}
export {get163HotPlayList, get163PlayListDetail,get163PlayMusic
    ,login163Music,login163Status,get163Search,get163SingerDetail
    ,get163AlbumDetail,get163Catlist,get163CatPlayList,
    get163TopList,get163SingerByCat,get163lyric

}